package cn.farm.farm.Controller.OrdinaryController;

import cn.farm.farm.Aop.IncrHotMessage;
import cn.farm.farm.Aop.IncrHotProtected;
import cn.farm.farm.Aop.UpdatePVAndUv;
import cn.farm.farm.Entity.Common.Message;
import cn.farm.farm.Entity.Common.Protection;
import cn.farm.farm.Entity.Ordinary.PVAndUV;
import cn.farm.farm.MessageModel.Common;
import cn.farm.farm.MessageModel.ListMessage;
import cn.farm.farm.MessageModel.PvMessage;
import cn.farm.farm.MessageModel.StringCommon;
import cn.farm.farm.Service.CommonService.MessageService;
import cn.farm.farm.Service.CommonService.ProtectService;
import cn.farm.farm.Util.redis.JedisAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("farm/common/count")
public class CountController {

    @Autowired
    JedisAdapter jedisAdapter;

    @Autowired
    ProtectService protectService;

    @Autowired
    MessageService messageService;

    @Value("${Jedis.hotMessageKey}")
    String hotMessageKey;

    @Value("${Jedis.protectKey}")
    String hotProtectKey;

    @Value("${Jedis.PV}")
    String PV;

    @Value("${Jedis.UV}")
    String UV;

    @UpdatePVAndUv
    @GetMapping("hotMessage")
    public Common gethotMessage(HttpSession session){
        log.info("查询热点咨询:"+hotMessageKey);
        Set<String> zrange = jedisAdapter.zrevrange(hotMessageKey, 0, 9);
        List<Message> messages = messageService.foundHotMessage(zrange);
        return new ListMessage<>(200,"ok",messages);
    }

    @GetMapping("hotProtected")
    public Common gethotProtected(){
        log.info("查询热点植保信息:"+hotProtectKey);
        Set<String> zrange = jedisAdapter.zrevrange(hotProtectKey, 0, 9);
        List<Protection> protections = protectService.foundHotProtect(zrange);
        return new ListMessage<>(200,"ok",protections);
    }

    @GetMapping("PVAndUV")
    public Common getPvAndUv(){
        return new PvMessage(200,"ok",new PVAndUV(jedisAdapter.get(PV),jedisAdapter.pfcount(UV)+""));
    }

    @IncrHotMessage
    @GetMapping("countMessage")
    public Common intoMessage(int messageID){
        return new StringCommon(200,"ok",jedisAdapter.zscore(hotMessageKey,messageID+"")+"");
    }


    @IncrHotProtected
    @GetMapping("countProtect")
    public Common intoProtect(int protectID){
        return new StringCommon(200,"ok",jedisAdapter.zscore(hotProtectKey,protectID+"")+"");
    }
}
